package com.example.kangjujiaofei.contants;

import cn.hutool.json.JSONUtil;
import lombok.Data;

import javax.servlet.ServletResponse;
import java.io.PrintWriter;
import java.io.Serializable;

@Data
public class ApiResponse<T> implements Serializable {

	private static final long serialVersionUID = 1189991146334068265L;
	
	public static final ApiResponse<Object> EXCEPTION = new ApiResponse<>(ResponseInfo.EXCEPTION);
	
	public static final ApiResponse<Object> ERROR = new ApiResponse<>(ResponseInfo.ERROR);

	public static final ApiResponse<Object> SUCCESS = new ApiResponse<>();

	public static final ApiResponse<Object> NOT_FOUND = new ApiResponse<>(ResponseInfo.NOT_FOUND);

	public static final ApiResponse<Object> AUTH_ACCOUNT_NOT_EXIT = new ApiResponse<>(ResponseInfo.AUTH_ACCOUNT_NOT_EXIT);

	public static final ApiResponse<Object> AUTH_PASSWORD_WRONG = new ApiResponse<>(ResponseInfo.AUTH_PASSWORD_WRONG);

	public static final ApiResponse<Object> AUTHENTICATION_FAILURE = new ApiResponse<>(ResponseInfo.AUTHENTICATION_FAILURE);

	public static final ApiResponse<Object> ILLEGAL_PARAM = new ApiResponse<>(ResponseInfo.ILLEGAL_PARAM);

	public static final ApiResponse<Object> AUTH_FAIL = new ApiResponse<>(ResponseInfo.AUTH_FAIL);

	public static final ApiResponse<Object> SIGN_NOT_MATCH = new ApiResponse<>(ResponseInfo.SIGN_MISMATCHING);
	
	public static final ApiResponse<Object> PARAM_MISSING = new ApiResponse<>(ResponseInfo.PARAM_MISSING);
	
	public static final ApiResponse<Object> VALIDATE_MSG = new ApiResponse<>(ResponseInfo.VALIDATE_MSG);

	public static final ApiResponse<Object> MESSAGE_IS_NULL = new ApiResponse<>(ResponseInfo.MESSAGE_IS_NULL);
	
	public static final ApiResponse<Object> VALIDATE_LOST = new ApiResponse<>(ResponseInfo.VALIDATE_LOST);

	public static final ApiResponse<Object> INVALID_PARAM = new ApiResponse<>(ResponseInfo.INVALID_PARAM);

	public static final ApiResponse<Object> NOTICE_SUCCESS = new ApiResponse<>(ResponseInfo.NOTICE_SUCCESS);

	public static final ApiResponse<Object> TASK_NOT_EXIST = new ApiResponse<>(ResponseInfo.TASK_NOT_EXIST);

	public static final ApiResponse<Object> PROJECT_ID_NOT_EXIST = new ApiResponse<>(ResponseInfo.PROJECT_ID_NOT_EXIST);

	public static final ApiResponse<Object> PROJECT_ID_RELATED = new ApiResponse<>(ResponseInfo.PROJECT_ID_RELATED);

	public static final ApiResponse<Object> SAVE_VEHICLE_FAIL = new ApiResponse<>(ResponseInfo.SAVE_VEHICLE_FAIL);

	public static final ApiResponse<Object> BANK_ACCOUNT_CREATE_FAIL = new ApiResponse<>(ResponseInfo.BANK_ACCOUNT_CREATE_FAIL);

	public static final ApiResponse<Object> BANK_ACCOUNT_CREATE_EXIT = new ApiResponse<>(ResponseInfo.BANK_ACCOUNT_CREATE_EXIT);

	// private Page<?> page;
	
	private int code; // 返回响应码:0=成功返回，其他=错误返回

	private String msg; // 返回响应消息

	private T data; // 返回数据

	
	public static ApiResponse<Object> getInstance(Object data) {
		ApiResponse<Object> response = new ApiResponse<>();
		return response.addData(data);
	}

	public static ApiResponse<Object> getInstance(Integer code){
		ApiResponse<Object> res = new ApiResponse<>();
		res.setCode(code);
		return res;
	}

	public static ApiResponse<Object> getInstance(Integer code, Object data){
		ApiResponse<Object> res = new ApiResponse<>();
		res.setData(data);
		res.setCode(code);
		return res;
	}

	public static ApiResponse<Object> getInstance(Integer code, String msg, Object data){
		ApiResponse<Object> res = new ApiResponse<>();
		res.setCode(code);
		res.setMsg(msg);
		res.setData(data);
		return res;
	}

	public ApiResponse() {
		this(ResponseInfo.SUCCESS);
	}

	public ApiResponse(ResponseInfo responseInfo) {
		this.code = responseInfo.code;
		this.msg = responseInfo.msg;
	}
	
	public ApiResponse(Integer code, String msg) {
		this.code = code;
		this.msg = msg;
	}

	public ApiResponse<T> addData(T data) {
		this.data = data;
		return this;
	}
	
	// public ApiResponse addPage(Page<?> page) {
	//	this.setPage(page);
	//	return this;
	// }

	// public Page<?> getPage() {
	//	return page;
	// }

	// public void setPage(Page<?> page) {
	//	this.page = page;
	// }

	public static ApiResponse<Object> success(Object data){
		ApiResponse<Object> res = new ApiResponse<>();
		res.setData(data);
		res.setCode(ResponseInfo.SUCCESS.code);
		return res;
	}

	public static ApiResponse<Object> fail(String msg, Object data) {
		ApiResponse<Object> res = new ApiResponse<>();
		res.setCode(ResponseInfo.FAIL.code);
		res.setData(data);
		res.setMsg(msg);
		return res;
	}


	/**
	 * Response输出Json格式
	 *
	 * @param response
	 * @param data     返回数据
	 */
	public static void responseJson(ServletResponse response, Object data) {
		PrintWriter out = null;
		try {
			response.setCharacterEncoding("UTF-8");
			response.setContentType("application/json");
			out = response.getWriter();
			out.println(JSONUtil.toJsonStr(data));
			out.flush();
		} catch (Exception e) {
		} finally {
			if (out != null) {
				out.close();
			}
		}
	}
}
